package com.example.my.meila.view.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.my.meila.R;
import com.example.my.meila.api.ExceptionHandle;
import com.example.my.meila.common.image.ImageLoader;
import com.example.my.meila.constast.Constast;
import com.example.my.meila.entity.Paihangbang;
import com.example.my.meila.presenter.RankPresenter;
import com.example.my.meila.view.adapter.PaihangbangAdapter;
import com.example.my.meila.view.adapter.PaihangbangBottomAdapter;
import com.example.my.meila.view.adapter.PaihangbangCategoriesAdapter;
import com.example.my.meila.view.adapter.PaihangbangheadAdapter;
import com.example.my.meila.view.impl.RankView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class RankFragment extends Fragment implements RankView {
    @BindView(R.id.tverweima)
    TextView tverweima;
    private List<Paihangbang.DataBean.NationsBean> data = new ArrayList<>();
    /**
     * 扫描跳转Activity RequestCode
     */
    public static final int REQUEST_CODE = 111;

    private PaihangbangAdapter paihangbangAdapter;


    private Paihangbangheadhead paihangbangheadhead;
    private Paihangbanghead paihangbanghead;
    private PaihangbangBottom paihangbangBottom;
    private RankPresenter rankPresenter;

    @BindView(R.id.xrv_quanqiugou)
    XRecyclerView xrvQuanqiugou;

    public RankFragment() {
        // Required empty public constructor
    }

    public static RankFragment newInstance() {

        Bundle args = new Bundle();

        RankFragment fragment = new RankFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_pai_hang, container, false);
        ButterKnife.bind(this, view);
        rankPresenter = new RankPresenter(this);
        rankPresenter.getRankData();


        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        xrvQuanqiugou.setLayoutManager(llm);

        paihangbangAdapter = new PaihangbangAdapter(data, getContext());
        xrvQuanqiugou.setAdapter(paihangbangAdapter);

        paihangbanghead = new Paihangbanghead();
        xrvQuanqiugou.addHeaderView(paihangbanghead.view);

        paihangbangBottom = new PaihangbangBottom();
        xrvQuanqiugou.addFootView(paihangbangBottom.view);


        return view;
    }


    @Override
    public void onSuccess(Paihangbang paihangbang) {
        if (paihangbang != null) {
            data.addAll(paihangbang.getData().getNations());
            paihangbanghead.data.addAll(paihangbang.getData().getFeature_show());
            paihangbangheadhead.data.addAll(paihangbang.getData().getCategories());
            paihangbangBottom.data.addAll(paihangbang.getData().getAdvisers().getList());
            ImageLoader.getInstance().display(getContext(), Constast.PUBLIC_URL + paihangbang.getData().getTop().getCustom().get(0).getImg().getImg(), paihangbangheadhead.ivPaihangbangNiandubang
            );


            paihangbangBottom.paihangbangBottomAdapter.notifyDataSetChanged();
            paihangbangheadhead.paihangbangCategoriesAdapter.notifyDataSetChanged();
            paihangbanghead.paihangbangheadAdapter.notifyDataSetChanged();
            paihangbangAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    @OnClick(R.id.tverweima)
    public void onClick() {
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    public class Paihangbangheadhead {
        View view;
        private PaihangbangCategoriesAdapter paihangbangCategoriesAdapter;
        @BindView(R.id.iv_paihangbang_niandubang)
        ImageView ivPaihangbangNiandubang;
        @BindView(R.id.rv_paihangbang_categories)
        RecyclerView rvPaihangbangCategories;

        private List<Paihangbang.DataBean.CategoriesBean> data = new ArrayList<>();

        public Paihangbangheadhead() {
            view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_paihangbang_head_head, null);
            ButterKnife.bind(this, view);
            paihangbangCategoriesAdapter = new PaihangbangCategoriesAdapter(getContext(), data);
            LinearLayoutManager llm = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
            rvPaihangbangCategories.setLayoutManager(llm);
            rvPaihangbangCategories.setAdapter(paihangbangCategoriesAdapter);


        }
    }


    public class Paihangbanghead {
        View view;
        private PaihangbangheadAdapter paihangbangheadAdapter;
        @BindView(R.id.xrv_quanqiugou_head)
        XRecyclerView xrvQuanqiugouHead;
        @BindView(R.id.tv_quanqiugou_more)
        TextView tvQuanqiugouMore;
        List<Paihangbang.DataBean.FeatureShowBean> data = new ArrayList<>();

        public Paihangbanghead() {
            view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_paihangbang_head, null);
            ButterKnife.bind(this, view);
            paihangbangheadAdapter = new PaihangbangheadAdapter(getContext(), data);
            LinearLayoutManager llm = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
            xrvQuanqiugouHead.setLayoutManager(llm);
            xrvQuanqiugouHead.setAdapter(paihangbangheadAdapter);
            paihangbangheadhead = new Paihangbangheadhead();
            xrvQuanqiugouHead.addHeaderView(paihangbangheadhead.view);
        }
    }

    public class PaihangbangBottom {
        View view;
        @BindView(R.id.rv_paihangbang_bottom)
        RecyclerView rvPaihangbangBottom;
        private PaihangbangBottomAdapter paihangbangBottomAdapter;
        List<Paihangbang.DataBean.AdvisersBean.ListBean> data = new ArrayList<>();


        public PaihangbangBottom() {
            view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_paihangbang_bottom, xrvQuanqiugou, false);
            ButterKnife.bind(this, view);
            paihangbangBottomAdapter = new PaihangbangBottomAdapter(getContext(), data);
            GridLayoutManager llm = new GridLayoutManager(getContext(), 3);
            rvPaihangbangBottom.setLayoutManager(llm);
            rvPaihangbangBottom.setAdapter(paihangbangBottomAdapter);
        }
    }

    @Override
   public void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE)
                        == CodeUtils.RESULT_SUCCESS) {
                    String result =
                            bundle.getString(CodeUtils.RESULT_STRING);
                    if(result.length()==13){
                    Toast.makeText(getActivity(),
                            "扫描二维码成功"+result, Toast.LENGTH_LONG).show();}
                    else {
                    //用默认浏览器打开扫描得到的地址
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(result.toString());
                    intent.setData(content_url);
                    startActivity(intent);}
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE)
                        == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(getActivity(),
                            "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}